package com.victor.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.tomcat.jni.File;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.victor.utils.Msg;
import com.victor.utils.ffmpeg.VideoThumbTaker;

@RestController
@RequestMapping("/video")
public class CutVideo {

	@RequestMapping("/getvideophoto")
	public Msg toPhoto(String videoname) {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String dateStr = sdf.format(date);
		ServletRequestAttributes aRequestAttributes = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		HttpServletRequest request = aRequestAttributes == null ? null : aRequestAttributes.getRequest();
		String webpath = request.getServletContext().getRealPath("/");
		java.io.File file = new java.io.File(webpath + "\\" + "videophoto");
		if (!file.exists()) {
			file.mkdir();
		}
		String url = webpath + "fileUpload" + "\\" + dateStr + "\\" + "file" + "\\" + videoname;
		VideoThumbTaker videoThumbTaker = new VideoThumbTaker("C:\\ffmpeg\\bin\\ffmpeg.exe");
		try {
			String photo1 = UUID.randomUUID().toString() + ".png";
			String photo2 = UUID.randomUUID().toString() + ".png";
			String photo3 = UUID.randomUUID().toString() + ".png";
			videoThumbTaker.getThumb(url, file + "\\" +photo1, 800, 600, 0, 0, 3);
			videoThumbTaker.getThumb(url, file + "\\" + photo2, 800, 600, 0, 0, 6);
			videoThumbTaker.getThumb(url, file + "\\" +photo3, 800, 600, 0, 0, 9);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return Msg.success();
	}

}
